﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using Modbus.Device;
using System.Xml.Linq;

namespace thinger
{
    /// <summary>
    /// 基于modbus4开源库的二次封装
    /// </summary>
    class ModbusRtu
    {
        private SerialPort serialPort;
        private ModbusSerialMaster master;


        public bool connect(string portName, int baudRate, Parity parity, int dataRate, StopBits stopBits)
        {
            if (serialPort != null && this.serialPort.IsOpen)
            {
                this.serialPort.Close();
            }

            try
            {
                this.serialPort = new SerialPort(portName, baudRate, parity, dataRate, stopBits);
                this.serialPort.Open();
                this.master = ModbusSerialMaster.CreateRtu(this.serialPort);
                return true;



            }
            catch (Exception ex)
            {
                Exception ex11 = new Exception('' + ex.Message);
                throw ex11;
            }

        }

        public void disconnet()
        {
            if (this.serialPort != null && this.serialPort.IsOpen)
            {
                this.serialPort.Close();
            }
            master = null;
        }
        /// <summary>
        /// 读取输出线圈
        /// </summary>
        /// <param name="slaveAddress">设备地址</param>
        /// <param name="startAddress">起始地址</param>
        /// <param name="numberOfPoints">读取长度</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool[] ReadOutPutcoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {

            try
            {
                return this.master.ReadCoils(slaveAddress, startAddress, numberOfPoints);
            }
            catch (Exception ex)
            {

                throw new Exception("读取输出线圈"+ex.Message);
            }
        }
        /// <summary>
        /// 读取输入线圈
        /// </summary>
        /// <param name="slaveAddress"></param>
        /// <param name="startAddress"></param>
        /// <param name="numberOfPoints"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public bool[] ReadInputsCoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {

            try
            {
                return this.master.ReadInputs(slaveAddress, startAddress, numberOfPoints);
            }
            catch (Exception ex)
            {

                throw new Exception("读取输入线圈"+ex.Message);
            }
        
        }
        /// <summary>
        /// 读取输入寄存器
        /// </summary>
        /// <param name="slaveAddress"></param>
        /// <param name="startAddress"></param>
        /// <param name="numberOfPoints"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public Byte[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints)
        {

            try
            {
               ushort[] data= this.master.ReadHoldingRegisters(slaveAddress, startAddress, numberOfPoints);
               List<Byte> registers= new List<Byte>();
                foreach (var item in data)
                {
                    registers.AddRange(BitConverter.GetBytes(item));
                }

                return registers.ToArray(); 
            }
            catch (Exception ex)
            {

                throw new Exception("读取输入寄存器"+ex.Message);
            }

        }






    }
}
